package com.galleryviewpager.d_chart;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.ArrayList;


public class SimpleLineChartView extends View {

    private int mWidth;
    private int mHeight;

    private String[] mXAxis = {};

    private String[] mYAxis = {};

    private Paint axisPaint = new Paint();

    private ArrayList<Integer> mPointMap;

    //x轴的刻度集合
    private ArrayList<Integer> xPoints = new ArrayList<>();

    private ArrayList<Integer> yPoints = new ArrayList<>();

    private float mYAxisFontSize = 24;

    private Paint pointPaint = new Paint();

    private Paint linePaint = new Paint();

    public SimpleLineChartView(Context context) {
        this(context, null);
    }

    public SimpleLineChartView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public SimpleLineChartView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initData();
    }

    private void initData() {
        mYAxis = new String[]{"10k", "20k", "30k", "40k", "50k"};
        mXAxis = new String[]{"1", "2", "3", "4", "5", "6", "7"};
        mPointMap = new ArrayList<>();
        for (int i = 0; i < 10; i++) {
            mPointMap.add(((int) (Math.random() * 5)));
        }
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        //todo 这里只做exactly 的写法
        int widthMode = MeasureSpec.getMode(widthMeasureSpec);
        if (widthMode == MeasureSpec.EXACTLY) {
            mWidth = MeasureSpec.getSize(widthMeasureSpec);
        }

        int heightMode = MeasureSpec.getMode(heightMeasureSpec);
        if (heightMode == MeasureSpec.EXACTLY) {
            mHeight = MeasureSpec.getSize(heightMeasureSpec);
        }
        setMeasuredDimension(mWidth, mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        if (mXAxis.length == 0 || mYAxis.length == 0) {
            return;
        }

        axisPaint.setTextSize(mYAxisFontSize);

        if (mPointMap == null || mPointMap.size() == 0) {
            String noData = "没有数据";
            float textLength = axisPaint.measureText(noData);
            canvas.drawText(noData, (mWidth - textLength) / 2, mHeight / 2, axisPaint);
        } else {
            //画y轴
            int yInterval = (int) ((mHeight - mYAxisFontSize - 2) / mYAxis.length);
            for (int i = 0; i < mYAxis.length; i++) {
                int yPoint = (int) (mYAxisFontSize + i * yInterval);
                canvas.drawText(mYAxis[i], 0, yPoint, axisPaint);
                yPoints.add(yPoint);
            }

            //画x轴
            //计算y轴开始的原点坐标
            int xItemX = (int) axisPaint.measureText(mYAxis[1]);

            //x轴的偏移量
            int xOffset = 50;
            //计算x轴刻度间距
            int xInterval = (int) ((mWidth - xOffset) / mXAxis.length);

            //获取x轴刻度Y坐标
            int xItemY = (int) (mYAxisFontSize + mYAxis.length * yInterval);

            for (int i = 0; i < mXAxis.length; i++) {
                canvas.drawText(mXAxis[i], i * xInterval + xItemX + xOffset, xItemY, axisPaint);
                int xPoint = (int) (i * xInterval + xItemX + axisPaint.measureText(mXAxis[i]) / 2 + xOffset + 10);
                xPoints.add(xPoint);
            }

            //画点

            pointPaint.setColor(Color.RED);
            pointPaint.setStyle(Paint.Style.FILL);
            linePaint.setColor(Color.GREEN);
            linePaint.setStrokeWidth(5f);
            linePaint.setStyle(Paint.Style.FILL);

            for (int i = 0; i < mXAxis.length; i++) {
                canvas.drawCircle(xPoints.get(i), yPoints.get(mPointMap.get(i)), 10f, pointPaint);
                if (i > 0) {
                    canvas.drawLine(xPoints.get(i - 1), yPoints.get(mPointMap.get(i - 1)), xPoints.get(i), yPoints.get(mPointMap.get(i)), linePaint);
                }
            }
        }

    }
}
